//
//  UserInfoController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/14.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "UserInfoController.h"

//
#import "BCUserInfoBaseCell.h"
#import "BCUserInfoHeadPortraitCell.h"
#import "BCUserInfoNameCell.h"

//
#import "BCUserHeadPortraitController.h"//头像控制器
#import "BCUserNameController.h"//修改名字

//
#import "BCSettingLogoutView.h"//退出登录的视图

//request
#import "BCUserLogoutRequest.h"//请求退出登录

@interface UserInfoController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *infoTable;

/**
 *BCSettingLogoutView
 */
@property (nonatomic, strong) BCSettingLogoutView *logoutView;


/**
 *dataSource
 */
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation UserInfoController

- (BCSettingLogoutView *)logoutView {
    if (!_logoutView) {
        _logoutView = [[BCSettingLogoutView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [BCSettingLogoutView defaultHeight])];
        [_logoutView.logoutButton addTarget:self action:@selector(logoutButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        //
        [_dataSource addObject:[BCUserInfoCellModel headPortrait]];
        [_dataSource addObject:[BCUserInfoCellModel name]];
        if (self.centerData.user) {
            BCUserInfoCellModel *uid = [BCUserInfoCellModel UID];
            [_dataSource addObject:uid];
            //
            BCUserInfoCellModel *code = [BCUserInfoCellModel recommendCode];
            code.title = self.centerData.flow.share_code;
            [_dataSource addObject:code];
        }
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.title = BCLocalizedKey(@"uc_user_info");
    //
    [self setupSubviews];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //
    [self.infoTable reloadData];
}


- (void)setupSubviews {
    self.infoTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.infoTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.infoTable.dataSource = self;
    self.infoTable.delegate = self;
    [self.view addSubview:self.infoTable];
    [self.infoTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    for (NSString *cellName in [BCUserInfoCellModel registerCells]) {
        [self.infoTable registerClass:NSClassFromString(cellName) forCellReuseIdentifier:cellName];
    }
    [self.infoTable setTableFooterView:self.logoutView];
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCUserInfoCellModel *cellModel = self.dataSource[indexPath.row];
    BCUserInfoBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(cellModel.cellClass)];
    cell.cellModel = cellModel;
    cell.tableView = tableView;
    cell.centerData = self.centerData;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCUserInfoCellModel *cellModel = self.dataSource[indexPath.row];
    if (cellModel.cellClass==BCUserInfoHeadPortraitCell.class) {
        return [BCUserInfoHeadPortraitCell defaultHeight];
    }
    return [BCUserInfoBaseCell defaultHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BCUserInfoCellModel *cellModel = self.dataSource[indexPath.row];
    if (cellModel.cellClass==BCUserInfoHeadPortraitCell.class) {
        [self toOperationHeadPortraitAction];
    }else if (cellModel.cellClass==BCUserInfoNameCell.class) {
        [self toOperationNameAction];
    }
}


#pragma mark - actions
//操作头像
- (void)toOperationHeadPortraitAction {
    //获取头像
    UserInfoModel *userInfo = [BCUserManager sharedUser].userInfo;
    UIImage *cachaHeadImage;
    if (userInfo.head_img.length) {//有头像地址
        cachaHeadImage = [[SDImageCache sharedImageCache] imageFromCacheForKey:userInfo.head_img];
    }else {//第一次注册可能没有头像
        
    }
    BCUserHeadPortraitController *headPortraitVC = [BCUserHeadPortraitController new];
    headPortraitVC.headImage = cachaHeadImage;
    headPortraitVC.headUrl = userInfo.head_img;
    [self.navigationController pushViewController:headPortraitVC animated:true];
}
//操作名字
- (void)toOperationNameAction {
    BCUserNameController *nameVC = [BCUserNameController new];
    nameVC.currentName = [BCUserManager sharedUser].userInfo.nickname;
    [self.navigationController pushViewController:nameVC animated:true];
}

#pragma mark - actions
//退出按钮点击了
- (void)logoutButtonDidClickedAction {
    [self requestLogoutAction];
}


- (void)requestLogoutAction {
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    [QMUITips showLoading:BCLocalizedKey(@"Waiting logout") inView:keyW];
    BCUserLogoutRequest *request = [BCUserLogoutRequest new];
    WEAK_SELF
    [request didRequestCompleted:^{
        [QMUITips hideAllTipsInView:keyW];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        [[BCUserManager sharedUser] logout];
        [wself.navigationController popViewControllerAnimated:true];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *errMsg = error.domain;
            [QMUITips showError:errMsg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errMsg]];
        }
    }];
    [request requestServer];
}

@end
